import { USER_ACTION_TYPES } from '../actionTypes';

const initialState = {
  userInfo: null,
  token: null,
  shopId: null, 
  isLogin: false,
  loading: false,
  error: null
};

const userReducers = (state = initialState, action) => {
  switch (action.type) {
    case USER_ACTION_TYPES.LOGIN_START:
      return {
        ...state,
        loading: true,
        error: null
      };

    case USER_ACTION_TYPES.LOGIN_SUCCESS:
      return {
        ...state,
        userInfo: action.payload.userInfo,
        token: action.payload.token,
        isLogin: true,
        loading: false,
        error: null
      };

    case USER_ACTION_TYPES.LOGIN_FAILURE:
      return {
        ...state,
        userInfo: null,
        token: null,
        isLogin: false,
        loading: false,
        error: action.payload
      };

    case USER_ACTION_TYPES.LOGOUT:
      return {
        ...state,
        userInfo: null,
        token: null,
        shopId: null,
        isLogin: false,
        loading: false,
        error: null
      };

    case 'SET_SHOP_ID':
      return {
        ...state,
        shopId: action.payload
      };

    case USER_ACTION_TYPES.UPDATE_USER_INFO:
      return {
        ...state,
        userInfo: {
          ...state.userInfo,
          ...action.payload
        }
      };

    case USER_ACTION_TYPES.CLEAR_ERROR:
      return {
        ...state,
        error: null
      };

    default:
      return state;
  }
};

export default userReducers;
